﻿#ifndef PROCESSINFOSTARTUCT_H
#define PROCESSINFOSTARTUCT_H
#include <QObject>
#include <QProcess>
#include <QDateTime>
#include "ProcessInfoStartuct_global.h"

// 进程消息

class PROCESSINFOSTARTUCT_EXPORT ProcessInfoStartuct : public QObject {
Q_OBJECT;
public:
	struct PROCESSINFOSTARTUCT_EXPORT CallData {
		// 调用间隔
		QTime timeOut;
		// 大于限界判定
		QTime Greater;
		// 小于限界判定
		QTime Less;
		// 重启间隔
		QTime reStartTimeOut;
		// 运行计数
		qint64 count = -1;
		// 重启计数
		qint32 reStart = 0;
		// 进程的策略
		qint32 forceClose = 0;
	};

public:
	ProcessInfoStartuct(QObject* parent = Q_NULLPTR);
private:
	CallData* callData = Q_NULLPTR;
	QProcess* process = Q_NULLPTR;
	QString processAbsolteFilePath;
	QStringList processArguments;
public:
	CallData* getCallData( ) const {
		return callData;
	}

	ProcessInfoStartuct(const ProcessInfoStartuct& other) {
		QObject(other.parent( ));
		callData = new CallData(*other.callData);
		process = new QProcess(other.process);
		processAbsolteFilePath = other.processAbsolteFilePath;
		processArguments = other.processArguments;
	}

	ProcessInfoStartuct& operator=(const ProcessInfoStartuct& other) {
		if( this == &other )
			return *this;
		QObject(other.parent( ));
		callData = new CallData(*other.callData);
		process = new QProcess(other.process);
		processAbsolteFilePath = other.processAbsolteFilePath;
		processArguments = other.processArguments;
		return *this;
	}

	void setCallData(CallData* callData) {
		this->callData = callData;
	}

	QProcess* getProcess( ) const {
		return process;
	}

	void setProcess(QProcess* process) {
		this->process = process;
	}

	QString getProcessAbsolteFilePath( ) const {
		return processAbsolteFilePath;
	}

	void setProcessAbsolteFilePath(const QString& processAbsolteFilePath) {
		this->processAbsolteFilePath = processAbsolteFilePath;
	}

	QStringList getProcessArguments( ) const {
		return processArguments;
	}

	void setProcessArguments(const QStringList& processArguments) {
		this->processArguments = processArguments;
	}

	int start( ) {
		if( process == Q_NULLPTR ) {
			process = creatorProcessObj( );
		}
		if( callData->count != 0 ) {
			process->start(processAbsolteFilePath, processArguments);
			if( callData->count > 0 )
				callData->count -= 1;
			return callData->count;
		}
		return 0;
	}

	~ProcessInfoStartuct( ) {
		if( callData )
			delete callData;
		if( process )
			delete process;
	}

private:
	QProcess* creatorProcessObj( ) {
		QProcess* processObj = new QProcess;
		connect(processObj, &QProcess::started, this, &ProcessInfoStartuct::started);
		void (QProcess::*pFunFinished)(int exitCode
			, QProcess::ExitStatus exitStatus) = &QProcess::finished;
		connect(processObj, pFunFinished, this, &ProcessInfoStartuct::finished);
		connect(processObj, &QProcess::readyReadStandardOutput, this, &ProcessInfoStartuct::readyReadStandardOutput);
		connect(processObj, &QProcess::stateChanged, this, &ProcessInfoStartuct::stateChanged);
		connect(processObj, &QProcess::errorOccurred, this, &ProcessInfoStartuct::errorOccurred);
		return processObj;
	}

signals:
	// 程序运行结束
	void finished(int exitCode
		, QProcess::ExitStatus exitStatus);
	// 从程序中获取输出
	void readyReadStandardOutput( );
	// 启动
	void started( );
	// 状态被改变
	void stateChanged(QProcess::ProcessState newState);
	// 错误退出
	void errorOccurred(QProcess::ProcessError error);
};
#endif // PROCESSINFOSTARTUCT_H
